import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class StorageConfigService extends BaseService<any> {
  private readonly logger = new Logger(StorageConfigService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getStorageList
   * 对应 PHP: StorageConfigService_admin::getStorageList()
   * 逻辑类型: undefined - undefined
   */
  async getStorageList() {
    // 基于PHP真实逻辑: getStorageList
    // PHP原文: $config_type = (new CoreStorageService())->getStorageConfig($this->site_id);         $storage_type_list = StorageDict::getType();         $list = []; ...
const config_type = this.coreStorageService.getStorageConfig(this.site_id];
        const storage_type_list = StorageDict.getType(];
        const list = [);
        foreach (storage_type_list as k => v) {
            const data = [];
            data.storage_type = k;
            data.is_use = k == config_type.default ? StorageDict.ON : StorageDict.OFF;
            data.name = (this.site_id != 0 && k == StorageDict.LOCAL) ? (v.site_name || v.name) : v.name;
            data.component = v.component;
            foreach (v.params as k_param => v_param) {
                const value = config_type[k][k_param] || '';
                const encrypt_params = v.encrypt_params || [];
                if (value !== '' && in_[k_param, encrypt_params)) value = CommonDict.ENCRYPT_STR;
                data.params[k_param] = [
                    name: v_param,
                    value: value
                ];
            }
            list[] = data;
  }

  /**
   * getStorageConfig
   * 对应 PHP: StorageConfigService_admin::getStorageConfig()
   * 逻辑类型: undefined - undefined
   */
  async getStorageConfig(storage_type: string) {
    // 基于PHP真实逻辑: getStorageConfig
    // PHP原文: $storage_type_list = StorageDict::getType();         if(!array_key_exists($storage_type, $storage_type_list)) throw new AdminException('OSS_TYPE_NOT_E...
const storage_type_list = StorageDict.getType(];
        if(!array_key_exists(storage_type, storage_type_list)) throw new AdminBusinessException('OSS_TYPE_NOT_EXIST'];
        const info = this.coreConfigService.getConfig(this.site_id, 'STORAGE');
        if(!info))
        {
            const config_type = [default: StorageDict.LOCAL];//初始化
  }

  /**
   * setStorageConfig
   * 对应 PHP: StorageConfigService_admin::setStorageConfig()
   * 逻辑类型: undefined - undefined
   */
  async setStorageConfig(storage_type: any[], data: any[]) {
    // 基于PHP真实逻辑: setStorageConfig
    // PHP原文: $storage_type_list = StorageDict::getType();         if(!array_key_exists($storage_type, $storage_type_list)) throw new AdminException('OSS_TYPE_NOT_E...
const storage_type_list = StorageDict.getType(];
        if(!array_key_exists(storage_type, storage_type_list)) throw new AdminBusinessException('OSS_TYPE_NOT_EXIST');
        if (storage_type != FileDict.LOCAL) {
            const domain = data.domain;
            if (!str_contains(domain, 'http://') && !str_contains(domain, 'https://')){
                throw new AdminBusinessException('STORAGE_NOT_HAS_HTTP_OR_HTTPS');
            }
  }
}
